package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Wdqd;
import com.ruoyi.system.domain.bo.CountNumBo;
import com.ruoyi.system.domain.bo.WdqdBo;
import com.ruoyi.system.service.IWdqdService;
import com.ruoyi.system.service.IYgService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Qdkb;
import com.ruoyi.system.service.IQdkbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗位信息Controller
 *
 * @author ruoyi
 * @date 2025-04-15
 */
@RestController
@RequestMapping("/system/qdkb")
public class QdkbController extends BaseController
{
    @Autowired
    private IQdkbService qdkbService;
    @Autowired
    private IWdqdService wdqdService;
    @Autowired
    private IYgService ygService;


    /**
     * 查询岗位信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:qdkb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Qdkb qdkb)
    {
//        SysUser sysUser = getLoginUser().getUser();
//        boolean contains1OR2=true;
//        if(sysUser.getRoles()!=null && sysUser.getRoles().size()!=0){
//            contains1OR2 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==1L || v.getRoleId()==2L);
//        }
//        if(!contains1OR2){
//            qdkb.setEnterUserId(getUserId());
//        }
        startPage();
        List<Qdkb> list = qdkbService.selectQdkbList(qdkb);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:qdkb:list')")
    @GetMapping("/listTwo")
    public TableDataInfo list1(Qdkb qdkb)
    {
        SysUser sysUser = getLoginUser().getUser();
        boolean contains1OR2=true;
        if(sysUser.getRoles()!=null && sysUser.getRoles().size()!=0){
            contains1OR2 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==1L || v.getRoleId()==2L);
        }

        if(!contains1OR2){
            qdkb.setEnterUserId(getUserId());
        }
        startPage();
        List<Qdkb> list = qdkbService.selectQdkbList(qdkb);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:qdkb:list')")
    @GetMapping("/listOne")
    public TableDataInfo listOne(Qdkb qdkb)
    {

        Long userId = getUserId();
        LambdaQueryWrapper<Wdqd> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Wdqd::getQdkbId);
        lambdaQueryWrapper.eq(Wdqd::getUserId,userId);
        lambdaQueryWrapper.eq(Wdqd::getStatus,'1');
        List<Wdqd> listIds = wdqdService.list(lambdaQueryWrapper);
        Long [] ids=listIds.stream().map(v->v.getQdkbId()).toArray(Long[]::new);
        qdkb.setQdkbIds(ids);
        startPage();
        List<WdqdBo> list1 = qdkbService.selectQdkbListOne(qdkb);
        Long [] qdkbIds=list1.stream().map(v->v.getQdkbId()).toArray(Long[]::new);
        Map<String,Integer> numList=new HashMap<>();
        if(qdkbIds!=null && qdkbIds.length>0){
             List<CountNumBo> countNumBos = ygService.selectcountNums(qdkbIds);
             if(countNumBos!=null && countNumBos.size()!=0){
                 for (CountNumBo c: countNumBos) {
                     numList.put(c.getQdkbId()+"_"+c.getStatusTwo(),c.getNum());
                 }
             }
        }
        for (WdqdBo w: list1) {
            w.setAboutFaceNum(numList.get(w.getQdkbId()+"_2"));
            w.setToFaceNum(numList.get(w.getQdkbId()+"_3"));
            w.setOfferNum(numList.get(w.getQdkbId()+"_4"));
            w.setEntryNum(numList.get(w.getQdkbId()+"_6"));
        }

        return getDataTable(list1);
    }

    /**
     * 导出岗位信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:qdkb:export')")
    @Log(title = "岗位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Qdkb qdkb)
    {
        List<Qdkb> list = qdkbService.selectQdkbList(qdkb);
        ExcelUtil<Qdkb> util = new ExcelUtil<Qdkb>(Qdkb.class);
        util.exportExcel(response, list, "岗位信息数据");
    }

    @PreAuthorize("@ss.hasPermi('system:qdkb:exportTwo')")
    @Log(title = "岗位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTwo")
    public void export2(HttpServletResponse response, Qdkb qdkb)
    {
        SysUser sysUser = getLoginUser().getUser();
        boolean contains1OR2=true;
        if(sysUser.getRoles()!=null && sysUser.getRoles().size()!=0){
            contains1OR2 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==1L || v.getRoleId()==2L);
        }
        if(!contains1OR2){
            qdkb.setEnterUserId(getUserId());
        }
        List<Qdkb> list = qdkbService.selectQdkbList(qdkb);
        ExcelUtil<Qdkb> util = new ExcelUtil<Qdkb>(Qdkb.class);
        util.exportExcel(response, list, "岗位信息数据");
    }

    /**
     * 获取岗位信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:qdkb:query')")
    @GetMapping(value = "/{qdkbId}")
    public AjaxResult getInfo(@PathVariable("qdkbId") Long qdkbId)
    {
        return success(qdkbService.selectQdkbByQdkbId(qdkbId));
    }

    /**
     * 新增岗位信息
     */
    @PreAuthorize("@ss.hasPermi('system:qdkb:add')")
    @Log(title = "岗位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Qdkb qdkb)
    {
        qdkb.setEnterUserId(getUserId());
        return toAjax(qdkbService.insertQdkb(qdkb));
    }

    /**
     * 修改岗位信息
     */
    @PreAuthorize("@ss.hasPermi('system:qdkb:edit')")
    @Log(title = "岗位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Qdkb qdkb)
    {
        return toAjax(qdkbService.updateQdkb(qdkb));
    }

    /**
     * 删除岗位信息
     */
    @PreAuthorize("@ss.hasPermi('system:qdkb:remove')")
    @Log(title = "岗位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qdkbIds}")
    public AjaxResult remove(@PathVariable Long[] qdkbIds)
    {
        return toAjax(qdkbService.deleteQdkbByQdkbIds(qdkbIds));
    }
}
